<?php
// header("Cache-control: private"); // //IE 6 Fix 
// error_reporting (E_ALL); // I use this only for testing
include($_SERVER['DOCUMENT_ROOT']."/IphoneGoogleCode1/server/db.php");  // this path works for me...

// new since version 1.92: storage of sessions in MySQL
// if (USE_MYSQL_SESSIONS) {
	// include_once($_SERVER['DOCUMENT_ROOT']."/classes/access_user/session_handler.php"); 
// } else {
	// session_start();
// }

class pick_up {
	
	var $table_name = local; 
	var $street;
	var $country;
	var $postcode;
	var $town;
	var $long;
	var $lat;
	var $x;
	var $y;


	function getPostcodeApi($long,$lat)
	{
		
	}
	function getStreet($param) //get Street from table local
	{
		$sql = "SELECT street, town, postcode FROM local where street = '".$param."'";
 		$r = mysql_query($sql);
		if (!$r) {
			$this->last_error = mysql_error();
			return;
		} else {
				if (mysql_num_rows($r) == 0)
				{
					$this->getStreetGeo_gpc($param);
					return;
				}
			while ($row = mysql_fetch_row($r)) {
				echo "<hr>";
				echo  "<a href=#page3>". $row [0]."<br>";
				echo   $row[1]."&nbsp ";
				echo   $row[2]."</a><br>";
			 }
			mysql_free_result($r);
		}      
	}
	function getStreetGeo_gpc($param)
	{
		$sql = "SELECT street, town, postcode FROM geo_gpc where street LIKE '%".$param."%'";
 		$r = mysql_query($sql);
		if (!$r) {
			$this->last_error = mysql_error();
			return;
		} else {
				if (mysql_num_rows($r) == 0)
				{
					echo "Not Result street!";
					return;
				}
			while ($row = mysql_fetch_row($r)) {
				echo "<hr>";
				echo  "<a href=#page3>". $row [0]."<br>";
				echo   $row[1]."&nbsp ";
				echo   $row[2]."</a><br>";
			 }
			mysql_free_result($r);
		}      
	}
	function getLocaltion($param) //get Localtion from table local
	{
		$sql = "SELECT street, town,postcode FROM local where town LIKE '%".$param."%'";
		$r = mysql_query($sql);
		if (!$r) {
			$this->last_error = mysql_error();
			return;
		} else {
			if (mysql_num_rows($r) == 0)
			{
				echo "Not Result town!";
				return;
			}
			 while ($row = mysql_fetch_row($r))
			 {
				echo "<hr>";
				echo  "<a href=#page3>". $row [0]."<br>";
				echo   $row[1]."&nbsp ";
				echo   $row[2]."</a><br>";
			 }
			 mysql_free_result($r);
		}      
	}
	function getInfoApi($param) //Get Info (street,localtion)from API ..*: param1 is street,localtion or postcode
	{
		$url = 'http://geo.jamiethompson.co.uk/'.$param.'.xml';
		$parsedXML = simplexml_load_file($url);
			if($parsedXML->status != "200") {
				// echo "There has been a problem: " . $parsedXML->status;
				echo "Not result! ";
				return;
			}
			else
			{
				$this->postcode = $param;
				$this->country = $parsedXML->address->country;
				$this->town = $parsedXML->address->locality;
				$this->street = $parsedXML->address->street;
				$this->long = $parsedXML->geo->lng;
				$this->lat = $parsedXML->geo->lat;
				$this->x = $parsedXML->geo->os_x;
				$this->y = $parsedXML->geo->os_y;
				// echo "Street: ".$this->postcode;
				echo  "<a href=#page3>". $this->street."<br>";
				echo   $this->town."&nbsp ";
				echo   $param."</a>";
				$this->insetgeo_gpc(); 
			}
	}
	function getInfoLocal($param) // GEt info from local ..$param is poscode
	{
		$sql = "SELECT street,town FROM local where postcode ='".$param."'";
		$r = mysql_query($sql);
		if (!$r) {
			$this->last_error = mysql_error();
			return;
		} else {
			 $row = mysql_fetch_row($r);
			if ( !$row )
			{
				$this->getInfogeo_gpc($param);
			}
			else 
			{
			 echo  "<a href=#page3>". $row[0]."<br>";
			 echo   $row[1]."&nbsp ";
			 echo   $param."</a>";
			}
		}      
	}
	function getInfogeo_gpc($param) //get info from geo_gpc :* param is a postcode
	{
		$sql = "SELECT street,town FROM geo_gpc where postcode ='".$param."'";
		$r = mysql_query($sql);
		if (!$r) {
			$this->last_error = mysql_error();
			return;
		} else {
			 $row = mysql_fetch_row($r);
			if ( !$row )
			{
				$this->getInfoApi($param);
			}
			else 
			{
			 echo  "<a href=#page3>". $row[0]."<br>";
			 echo   $row[1]."&nbsp ";
			 echo   $param."</a>";
			}
		}      
	}
	function insetgeo_gpc() // insert info table geo_gpc from API 'function getInfoApi($param)'
	{
		$sql = "INSERT INTO geo_gpc values ('NULL''".$this->postcode."','".$this->street."','".$this->town."','".$this->lat."','".$this->long."','".$this->x."','".$this->y."')";
		// $sql = sprintf("INSERT INTO %s (id, login, pw, real_name, extra_info, email, access_level, active) VALUES (NULL, %s, %s, %s, %s, %s, %d, 'n')", 
		$r = mysql_query($sql);
		if (!$r) {
			$this->last_error = mysql_error();
         return;
		}
	}
	function getListDefault()
	{
		$sql = "SELECT company,postcode FROM `local` limit 10";
		$r = mysql_query($sql);
		if (!$r) {
			$this->last_error = mysql_error();
         return;
		} 
		else 
		{
			 while ($row = mysql_fetch_row($r))
			 {
				echo "<hr>";
				echo  "<a href=#page2-1>". $row[0]."<br></a>";
			}
			 mysql_free_result($r);
		}      
	}
}
?>